<!doctype html>
<html>

<head>
    <meta charset="utf-8">
    <meta name="viewport" content="maximum-scale=1.0,minimum-scale=1.0,user-scalable=0,width=device-width,initial-scale=1.0" />
    <meta name="format-detection" content="telephone=no,email=no,date=no,address=no">
    <title>Hello APP</title>
    <link rel="stylesheet" type="text/css" href="./css/api.css" />
</head>

<body>
</body>
<!-- 引入组件 -->
<script type="text/javascript" src="./script/less.min.js"></script>
<script type="text/javascript" src="./script/api.js"></script>
<script type="text/javascript">
    var connectionType, token, ajpush = null; //全局中定义变量
    var userToken = $api.getStorage('token');
    apiready = function() {
      ajpush = api.require('ajpush');
        //禁止导航页
        // api.removeLaunchView();

        // 手动关闭导航页
        setTimeout('api.removeLaunchView();', 2000);
        //禁止屏幕休眠
        // api.setKeepScreenOn({
        //     keepOn: true
        // });
        //获取网络状态
        fnGetConnetionStatus();
        // 在app中监听推送
        // SetListener()
            // 在iOS平台，使用极光推送发送通知时，若应用在前台运行，则推送内容可以通过setListener方法监听到，若应用在后台，系统会往设备通知栏发送通知，当通知被点击后，APICloud会将本次推送的内容通过事件监听回调的方式交给开发者
        // noticeClicked()

        api.getPrefs({      
            key: 'isWelcome'
        }, function(ret, err) {
            if (ret.value === '') {
                fnShowWelcome();            
                api.setPrefs({                
                    key: 'isWelcome',
                    value: true            
                })
            } else {
                var userToken = $api.getStorage('token');
                //判断缓存中是否存在，判断是否登录的状态。。。
                if (typeof(userToken) != undefined && typeof(userToken) != 'undefined' && userToken != null) {
                    fnOpenHeadPage()
                } else {
                    fnOpenLogin()
                }
            }
        })
    };

    // 监听推送
    function SetListener() {
            var ajpush = api.require('ajpush');
        ajpush.setListener(function(ret) {
            //a(JSON.stringify(ret))
            if (ret) {
                setBadge()
                var ajpushObj = ret.extras;
                var code = ajpushObj.code;
                var ORDERArr = JSON.parse(ajpushObj.data);
                console.log('接收到推送状态码：' + code);
                console.log('Data: ' + JSON.stringify(ORDERArr));
                api.confirm({
                    title: '车车侠',
                    msg: '是否查看新消息～',
                    buttons: ['确定', '取消']
                }, function(ret, err) {
                    if (ret.buttonIndex == 1) {
                        noticeToPage(code, ORDERArr)
                    } else {

                    }
                });
            }
        });
    }

    // 点击消息去掉小红点
    function setBadge() {
        ajpush.setBadge({
            badge: 0
        });
    }

    //点击消息
    function noticeClicked() {
        api.addEventListener({
            name: 'noticeclicked'
        }, function(ret, err) {
            console.log(JSON.stringify(ret));
            setBadge()
            if (ret) {
                var ajpushObj = ret.value.extras;
                var code = ajpushObj.code;
                var ORDERArr = JSON.parse(ajpushObj.data);
                console.log('接收到推送状态码：' + code);
                console.log('Data: ' + JSON.stringify(ORDERArr));
                noticeToPage(code, ORDERArr)
            }
        })
    }

    function noticeToPage(code, data) {
        console.log("Code: " + code);
        switch (code) {
            // 订单消息
            case '1':
                var types = 'ServiceCompletion';
                var param = {
                    orderID: data
                }
                showPage(types, param);
                break;
                // 派单页
            default:
        }
    }

    function showPage(names, param) {
        if (param) {
            api.openWin({
                name: names,
                url: 'widget://html/' + names + '.html',
                pageParam: param,
            })
        } else {
            api.openWin({
                name: names,
                url: 'widget://html/' + names + '.html',
            })
        }
    }

    // 获取token
    function getToken() {
        token = $api.getStorage('token');
        Headers = {
            'AUTHORIZATION': 'Bearer ' + token
        }
        return token;
    }

    // 网络状态
    function fnGetConnetionStatus() {
        connectionType = api.connectionType;
        //下面这几行代码就为最关键的，实时监听你网络的变化，来改变connectionType的
        //值，从而解决了舒心页面的问题
        api.addEventListener({
            name: 'online'
        }, function(ret, err) {
            connectionType = ret.connectionType;
        });
        api.addEventListener({
            name: 'offline'
        }, function(ret, err) {
            connectionType = "";
            fnOpenLogin();
            api.toast({
                msg: '您还未联网,请联网后重试',
                duration: 2000,
                location: 'bottom',
                global: true
            });
        });
    }
    // 主页
    function fnOpenHeadPage() {
        api.openWin({
            name: 'Main_Frame',
            url: 'html/Main_Frame.html',
            bounces: false,
            opaque: false,
            showProgress: true,
            vScrollBarEnabled: false,
            hScrollBarEnabled: false,
            slidBackEnabled: false,
            // delay: 300,
            animation: {
                type: 'ripple',
                duration: 1000
            }
        })
    }
    // 登录
    function fnOpenLogin() {
        api.openWin({
            name: 'Login',
            url: 'html/Login.html',
            bounces: false,
            opaque: false,
            showProgress: true,
            vScrollBarEnabled: false,
            hScrollBarEnabled: false,
            slidBackEnabled: false,
            // delay: 300,
            animation: {
                type: 'ripple',
                duration: 1000
            }
        })
    }

    function fnShowWelcome() {
        api.openWin({
            name: 'Guide',
            url: 'html/Guide.html',
            bounces: false,
            opaque: false,
            showProgress: true,
            vScrollBarEnabled: false,
            hScrollBarEnabled: false,
            slidBackEnabled: false,
            // delay: 300,
            animation: {
                type: 'ripple',
                duration: 1000
            }
        })
    }
</script>

</html>
